/**
* @author Ashish Gupta
* CVSSP, University of Surrey
* Copyright (c) 2010 Ashish Gupta
* All rights reserved.
*/
/**
 * 
 */
package experiment;

import genetic.GenePool;
import genetic.Population;
import hash.CellSpace;

import image.ImageSet;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;

import weka.classifiers.Evaluation;
import weka.classifiers.functions.SMO;
import weka.core.Instances;
import weka.core.converters.CSVLoader;

import configuration.GenePoolConfiguration;
import configuration.VisualRecognitionConfiguration;

/**
 * @author Ashish
 *
 */
public class runGA {

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {		
		VisualRecognitionConfiguration vrConfig = null;
		try {
			vrConfig = new VisualRecognitionConfiguration(args[0]);
		} catch (IOException e1) {
			System.out.println("Error in processing VisualRecognitionConfiguration File.");
			e1.printStackTrace();
		}
		
		GenePoolConfiguration genePoolConfig = null;
		try {
			genePoolConfig = new GenePoolConfiguration(args[1]);
		} catch (IOException e) {
			System.out.println("Error in processing GeneConfiguration File.");
			e.printStackTrace();
		}
				
		ImageSet imageSet = null;
		try {
			imageSet = new ImageSet(vrConfig, genePoolConfig);
		} catch (IOException e) {
			System.out.println("Error in initializing imageSet.");
			e.printStackTrace();
		}
		
		CellSpace cellSpace = null;
		try {
			cellSpace = new CellSpace(vrConfig,imageSet);
		} catch (IOException e) {
			System.out.println("Error in initializing cellSpace.");
			e.printStackTrace();
		}
		
		GenePool genePool = new GenePool(genePoolConfig, cellSpace, imageSet);
		
		File fitnessProfileFile = new File(vrConfig.fitnessProfileURL);
		FileWriter fitness_fw = new FileWriter(fitnessProfileFile);
		BufferedWriter fitness_bw = new BufferedWriter(fitness_fw);
		
		for(int i = 0; i < genePoolConfig.getNumGenerations(); i++){
			Population oldPopulation = genePool.getPopulation();
			genePool.getPopulation().evolve(oldPopulation, genePoolConfig, cellSpace, imageSet);
			genePool.getPopulation().selectFitesttestChromosome().getVisualCodebook().toCSVfile(vrConfig.codebookCSV);
			CSVLoader loader = new CSVLoader();
		    loader.setSource(new File(vrConfig.codebookCSV));
		    Instances data = loader.getDataSet();
		    data.setClassIndex(data.numAttributes()-1);
		    SMO svm = new SMO();
		    svm.buildClassifier(data);
		    Instances newData = data;
		    Evaluation eval = new Evaluation(newData);
		    eval.crossValidateModel(svm, newData, 1000, new Random(1));
		    System.out.println(eval.toSummaryString("\nResults\n====\n", false));
			double fitness = genePool.getPopulation().selectFitesttestChromosome().getFitness();
			System.out.println(i + " : " + fitness);
			fitness_bw.write(i + " : " + fitness);
		}
		fitness_bw.close();
		fitness_fw.close();
		
	}

}
